package main;

//Represents a currency
//(The rates are calculated by the only downloaded currency rates,
//those of the Euro)
public class Currency
{
    private String name;
    private float[] rates;
    
    public Currency(String name, float[] ratesData)
    {
        this.name=name;
        this.rates=new float[DataHandler.currencyNames.length];
        
        float thisCurrencyValueInEuros=ratesData[DataHandler.indexOfCurrency(this.name)];
        float theOtherCurrencyValueInEuros=0f;
        for (int i=0;i<ratesData.length;i++)
        {
            if (DataHandler.currencyNames[i].equals(this.name))
            {
                rates[i]=1f;
            }
            else
            {
                theOtherCurrencyValueInEuros=ratesData[DataHandler.indexOfCurrency(DataHandler.currencyNames[i])];
                rates[i]=theOtherCurrencyValueInEuros/thisCurrencyValueInEuros;
            }
        }
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public float[] getRates()
    {
        return rates;
    }
    
    public float getRateFor(String otherCurrency)
    {
        return rates[DataHandler.indexOfCurrency(otherCurrency)];
    }
    
    //Used as the body of an SMS
    public String toString()
    {
        String result="";
        result+="1 "+name+" =\n";
        String rateValue="";
        for (int i=0;i<DataHandler.currencyNames.length;i++)
        {
            if (!DataHandler.currencyNames[i].equals(name))
            {
                rateValue=getRates()[i]+"";
                if (rateValue.substring(rateValue.indexOf(".")+1).length()>5)
                {
                    rateValue=rateValue.substring(0,rateValue.indexOf(".")+1)+rateValue.substring(rateValue.indexOf(".")+1,rateValue.indexOf(".")+6);
                }
                result+=rateValue+" "+DataHandler.currencyNames[i]+"\n";
            }
        }
        return result;
    }
}
